/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.lifesting.wfs.runtime.impl;

import com.lifesting.wfs.model.ProcessDefinition;

import com.lifesting.wfs.runtime.ProcessInstance;
import com.lifesting.wfs.runtime.RuntimePackage;
import com.lifesting.wfs.runtime.Token;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Process Instance</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.lifesting.wfs.runtime.impl.ProcessInstanceImpl#getDefinition <em>Definition</em>}</li>
 *   <li>{@link com.lifesting.wfs.runtime.impl.ProcessInstanceImpl#getRootToken <em>Root Token</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ProcessInstanceImpl extends EObjectImpl implements ProcessInstance {
	/**
	 * The cached value of the '{@link #getDefinition() <em>Definition</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDefinition()
	 * @generated
	 * @ordered
	 */
	protected ProcessDefinition definition;

	/**
	 * The cached value of the '{@link #getRootToken() <em>Root Token</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRootToken()
	 * @generated
	 * @ordered
	 */
	protected Token rootToken;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ProcessInstanceImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return RuntimePackage.Literals.PROCESS_INSTANCE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ProcessDefinition getDefinition() {
		if (definition != null && definition.eIsProxy()) {
			InternalEObject oldDefinition = (InternalEObject)definition;
			definition = (ProcessDefinition)eResolveProxy(oldDefinition);
			if (definition != oldDefinition) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, RuntimePackage.PROCESS_INSTANCE__DEFINITION, oldDefinition, definition));
			}
		}
		return definition;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ProcessDefinition basicGetDefinition() {
		return definition;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setDefinition(ProcessDefinition newDefinition) {
		ProcessDefinition oldDefinition = definition;
		definition = newDefinition;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, RuntimePackage.PROCESS_INSTANCE__DEFINITION, oldDefinition, definition));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Token getRootToken() {
		return rootToken;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetRootToken(Token newRootToken, NotificationChain msgs) {
		Token oldRootToken = rootToken;
		rootToken = newRootToken;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN, oldRootToken, newRootToken);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setRootToken(Token newRootToken) {
		if (newRootToken != rootToken) {
			NotificationChain msgs = null;
			if (rootToken != null)
				msgs = ((InternalEObject)rootToken).eInverseRemove(this, RuntimePackage.TOKEN__PROCESS_INSTANCE, Token.class, msgs);
			if (newRootToken != null)
				msgs = ((InternalEObject)newRootToken).eInverseAdd(this, RuntimePackage.TOKEN__PROCESS_INSTANCE, Token.class, msgs);
			msgs = basicSetRootToken(newRootToken, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN, newRootToken, newRootToken));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN:
				if (rootToken != null)
					msgs = ((InternalEObject)rootToken).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN, null, msgs);
				return basicSetRootToken((Token)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN:
				return basicSetRootToken(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case RuntimePackage.PROCESS_INSTANCE__DEFINITION:
				if (resolve) return getDefinition();
				return basicGetDefinition();
			case RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN:
				return getRootToken();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case RuntimePackage.PROCESS_INSTANCE__DEFINITION:
				setDefinition((ProcessDefinition)newValue);
				return;
			case RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN:
				setRootToken((Token)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case RuntimePackage.PROCESS_INSTANCE__DEFINITION:
				setDefinition((ProcessDefinition)null);
				return;
			case RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN:
				setRootToken((Token)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case RuntimePackage.PROCESS_INSTANCE__DEFINITION:
				return definition != null;
			case RuntimePackage.PROCESS_INSTANCE__ROOT_TOKEN:
				return rootToken != null;
		}
		return super.eIsSet(featureID);
	}

} //ProcessInstanceImpl
